<?php
/**
 * 
 * 用户修改信息
 * @author alang
 *
 */

class Api_userinfoupdate_mdl extends CI_Model
{
	
	
	public function __construct()
	{
		parent::__construct();
	}
	
	/**
	 * username, sex, birthday, address, mobile
	 * 
	 * 输出
	 * @param Array $data
	 */
	public function outputdata($data)
	{
		if (!empty($data['EMAIL']) && !empty($data['USERID']) && !empty($data['TOKEN'])) {
			
			//检验是否登录
			$sessiondata = array();
			$sessiondata = $this->_checklogin($data['USERID'],$data['EMAIL'],$data['TOKEN']);
			
			if ($sessiondata) {
				$user = array();
				//登录验证成功
				!empty($data['BASEINFO']['USERNAME']) ? $user['txusername'] = $data['BASEINFO']['USERNAME'] : '';
				!empty($data['BASEINFO']['SEX']) ? $user['sex'] = $data['BASEINFO']['SEX'] : '';
				!empty($data['BASEINFO']['BIRTHDAY']) ? $user['birthday'] = strtotime($data['BASEINFO']['BIRTHDAY']) : '';
				!empty($data['BASEINFO']['MOBILE']) ? $user['mobile'] = $data['BASEINFO']['MOBILE'] : '';
				
				!empty($data['ADDRESS']['COUNTRY']) ? $user['txcountry'] = $data['ADDRESS']['COUNTRY'] : '';
				!empty($data['ADDRESS']['STATE']) ? $user['state'] = $data['ADDRESS']['STATE'] : '';
				!empty($data['ADDRESS']['STREET']) ? $user['txaddress'] = $data['ADDRESS']['STREET'] : '';
				!empty($data['ADDRESS']['CITY']) ? $user['city'] = $data['ADDRESS']['CITY'] : '';
				!empty($data['ADDRESS']['ZIP']) ? $user['zip'] = $data['ADDRESS']['ZIP'] : '';

				//投降修改
				if (!empty($data['FACEPHOTO']['IMAGEDATA']) && !empty($data['FACEPHOTO']['IMAGETYPE']) && in_array(strtolower($data['FACEPHOTO']['IMAGETYPE']), array('png','jpg','gif'))) {
 
					$img=base64_decode($data['FACEPHOTO']['IMAGEDATA']);  
					$imgname = $data['USERID'].'_'.time().'.'.strtolower($data['FACEPHOTO']['IMAGETYPE']);
					file_put_contents(SITE_ROOT.'/upload/users/'.$imgname, $img);
					$user['headpic'] = $imgname;
				}
				
				//修改用户资料
				if (!empty($user)){
				
					if ($this->_updateUserInfo($data['USERID'], $user)) {
						
						$outputdata['TAG'] = $data['TAG'];
						$outputdata['STATUS'] = 0;
						$outputdata['ERRORMESSAGE'] = 'SUCCESS';
						$outputdata['USERINFO'] = null;
						
					} else {
						
						$outputdata['TAG'] = $data['TAG'];
						$outputdata['STATUS'] = 1;
						$outputdata['ERRORMESSAGE'] = 'SYSTEM ERROR';
						$outputdata['USERINFO'] = null;
						
					}
					
				} else {
					
						$outputdata['TAG'] = $data['TAG'];
						$outputdata['STATUS'] = 1;
						$outputdata['ERRORMESSAGE'] = 'UPDATE NONTHING';
						$outputdata['USERINFO'] = null;
					
				}
				
			} else {
				//登录验证失败
				$outputdata['TAG'] = $data['TAG'];
				$outputdata['STATUS'] = 1;
				$outputdata['ERRORMESSAGE'] = 'NO LOGIN';
				$outputdata['USERINFO'] = null;
			}
			
		} else {
			
			$outputdata['TAG'] = $data['TAG'];
			$outputdata['STATUS'] = 1;
			$outputdata['ERRORMESSAGE'] = 'INFO ERROR';
			$outputdata['USERINFO'] = null;
		}

		header('Content-Type:text/html; charset=utf-8', false);
		echo json_encode($outputdata);
	}
	
	/**
	 * 
	 * 检验用户登录
	 * @param int $uid
	 * @param String $email
	 * @param String $token
	 */
	private function  _checklogin($uid,$email,$token)
	{
		
		$key = $uid.'_'.$email;
		$this->load->library('auth');
		$sessiondata = $this->session->userdata($key);
		
		if (empty($sessiondata)) {
			
			return false;
			
		} else {
			
			if ($token == $sessiondata['token']) {
				
				return true;
				
			} else {
				
				return false;
			}
			
		}
			
	}
	
	/**
	 * 
	 * 更新用户信息
	 * @param int $uid
	 * @param Array $user
	 */
	private function _updateUserInfo($uid,$user)
	{
		
		$this->db->where( 'uid', $uid );	
		return $this->db->update( 'cj_users', $user );
		
	}
	
	
}